package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.admin.pojo.Category;
import com.itheima.service.CategoryService;
import com.itheima.util.RequestParesUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author longbosheng
 * @Date 2022/6/13 19:36
 * @PackageName:com.itheima.controller
 * @ClassName: CategoryController
 * @Description: TODO
 * @Version 1.0
 */
@Log4j2
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加类别
     *
     * @param category
     * @param request
     * @return
     */
    @PostMapping
    public R insertCategory(@RequestBody Category category, HttpServletRequest request) {
        String id = RequestParesUtil.RequestPares(request);
        category.setCreateUser(id);
        category.setCreateTime(new Date());
        category.setUpdateUser(id);
        category.setUpdateTime(new Date());
        System.out.println("category的内容:"+category);
        return categoryService.insertCategory(category);
    }

    /**
     * 类别分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R showCategory(int page, int pageSize) {
       return categoryService.selectCategory(page, pageSize);
    }

    /**
     * 分类更新
     * @param category
     * @return
     */
    @PutMapping
    public R updateCategory(@RequestBody Category category,HttpServletRequest request){
        String id = RequestParesUtil.RequestPares(request);
        category.setUpdateUser(id);
        category.setUpdateTime(new Date());
        System.out.println(category.getName());
        return categoryService.updateCategory(category);
    }
    /**
     * 分类删除
     * @param id
     * @return
     */
    @DeleteMapping
    public R deleteCategory(String id){
        System.out.println("删除传入的信息"+id);
        return categoryService.deleteCategory(id);
    }

    /**
     * 下拉列表
     * @param type
     * @return
     */
    @GetMapping("list")
    public R DropDownList(int type){
        return categoryService.DropDownList(type);
    }

}
